<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>TCP 三次握手详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .step-number {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            padding: 20px;
            font-family: 'Consolas', 'Monaco', monospace;
            position: relative;
            overflow: hidden;
        }
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
        }
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .highlight-text {
            background: linear-gradient(180deg, transparent 60%, rgba(102, 126, 234, 0.3) 60%);
            padding: 0 4px;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <div class="floating-icon inline-block mb-6">
                <i class="fas fa-network-wired text-6xl opacity-90"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                TCP 三次握手
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                深入理解网络通信的基石，探索可靠连接建立的精妙过程
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-16">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <div class="flex items-start space-x-4">
                <div class="flex-shrink-0">
                    <div class="w-16 h-16 bg-purple-100 rounded-full flex items-center justify-center">
                        <i class="fas fa-handshake text-purple-600 text-2xl"></i>
                    </div>
                </div>
                <div class="flex-1">
                    <h2 class="text-2xl font-bold mb-4 text-gray-800">什么是 TCP 三次握手？</h2>
                    <p class="text-gray-600 leading-relaxed text-lg">
                        在 TCP（传输控制协议）中，<span class="highlight-text font-semibold">三次握手（Three-Way Handshake）</span>是建立可靠连接的关键过程。
                        它确保了通信双方可以安全地交换数据，并确认双方都准备好进行数据传输。这个精心设计的过程保证了网络通信的可靠性和安全性。
                    </p>
                </div>
            </div>
        </div>

        <!-- Visual Diagram -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h3 class="text-2xl font-bold mb-6 text-center text-gray-800">握手过程可视化</h3>
            <div class="mermaid">
                sequenceDiagram
                    participant C as 客户端
                    participant S as 服务器
                    
                    Note over C,S: 连接建立前
                    C->>S: 1. SYN (seq=100)
                    Note right of S: 服务器收到连接请求
                    S->>C: 2. SYN-ACK (seq=200, ack=101)
                    Note left of C: 客户端确认服务器响应
                    C->>S: 3. ACK (seq=101, ack=201)
                    Note over C,S: 连接建立成功！
            </div>
        </div>

        <!-- Three Steps -->
        <div class="grid md:grid-cols-3 gap-8 mb-12">
            <!-- Step 1 -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="text-center mb-6">
                    <span class="step-number text-6xl font-bold">01</span>
                </div>
                <h3 class="text-2xl font-bold mb-4 text-gray-800 flex items-center">
                    <i class="fas fa-paper-plane text-purple-600 mr-3"></i>
                    第一步：SYN
                </h3>
                <p class="text-gray-600 mb-4 leading-relaxed">
                    <strong>客户端发送 SYN 请求：</strong>连接的建立从客户端开始。客户端向服务器发送一个包含 SYN（同步）标志位的 TCP 数据包，请求建立连接。
                </p>
                <div class="code-block text-sm">
                    <div class="text-green-400 mb-1">// 客户端 → 服务器</div>
                    <div>[SYN, seq=100]</div>
                </div>
            </div>

            <!-- Step 2 -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="text-center mb-6">
                    <span class="step-number text-6xl font-bold">02</span>
                </div>
                <h3 class="text-2xl font-bold mb-4 text-gray-800 flex items-center">
                    <i class="fas fa-reply text-purple-600 mr-3"></i>
                    第二步：SYN-ACK
                </h3>
                <p class="text-gray-600 mb-4 leading-relaxed">
                    <strong>服务器回应 SYN-ACK：</strong>服务器收到请求后，发送包含 SYN 和 ACK 标志的数据包，表示同意建立连接并确认客户端的请求。
                </p>
                <div class="code-block text-sm">
                    <div class="text-blue-400 mb-1">// 客户端 ← 服务器</div>
                    <div>[SYN, ACK, seq=200, ack=101]</div>
                </div>
            </div>

            <!-- Step 3 -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="text-center mb-6">
                    <span class="step-number text-6xl font-bold">03</span>
                </div>
                <h3 class="text-2xl font-bold mb-4 text-gray-800 flex items-center">
                    <i class="fas fa-check-circle text-purple-600 mr-3"></i>
                    第三步：ACK
                </h3>
                <p class="text-gray-600 mb-4 leading-relaxed">
                    <strong>客户端确认 ACK：</strong>客户端收到服务器响应后，发送 ACK 确认包，确认服务器的 SYN 请求，完成连接建立。
                </p>
                <div class="code-block text-sm">
                    <div class="text-green-400 mb-1">// 客户端 → 服务器</div>
                    <div>[ACK, seq=101, ack=201]</div>
                </div>
            </div>
        </div>

        <!-- Why Three-Way Handshake -->
        <div class="bg-gradient-to-r from-purple-50 to-indigo-50 rounded-2xl p-8 mb-12">
            <h3 class="text-3xl font-bold mb-8 text-center text-gray-800">
                <i class="fas fa-question-circle text-purple-600 mr-3"></i>
                为什么需要三次握手？
            </h3>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl p-6 shadow-lg">
                    <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mb-4">
                        <i class="fas fa-user-check text-purple-600 text-xl"></i>
                    </div>
                    <h4 class="text-xl font-bold mb-3 text-gray-800">确认双方都准备好</h4>
                    <p class="text-gray-600 leading-relaxed">
                        三次握手确保客户端和服务器双方都准备好进行数据传输，双方交换序列号并确认对方能够接收数据。
                    </p>
                </div>
                <div class="bg-white rounded-xl p-6 shadow-lg">
                    <div class="w-12 h-12 bg-indigo-100 rounded-full flex items-center justify-center mb-4">
                        <i class="fas fa-sync text-indigo-600 text-xl"></i>
                    </div>
                    <h4 class="text-xl font-bold mb-3 text-gray-800">同步序列号</h4>
                    <p class="text-gray-600 leading-relaxed">
                        握手过程中交换的序列号帮助双方同步数据流的起始点，这确保了数据传输的可靠性和有序性。
                    </p>
                </div>
                <div class="bg-white rounded-xl p-6 shadow-lg">
                    <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mb-4">
                        <i class="fas fa-shield-alt text-purple-600 text-xl"></i>
                    </div>
                    <h4 class="text-xl font-bold mb-3 text-gray-800">防止旧连接干扰</h4>
                    <p class="text-gray-600 leading-relaxed">
                        通过交换初始序列号，三次握手防止了旧的连接请求干扰新的连接，确保了连接的唯一性和安全性。
                    </p>
                </div>
            </div>
        </div>

        <!-- Key Concepts -->
        <div class="bg-white rounded-2xl shadow-xl p-8">
            <h3 class="text-2xl font-bold mb-6 text-gray-800">
                <i class="fas fa-key text-purple-600 mr-3"></i>
                关键概念解释
            </h3>
            <div class="space-y-4">
                <div class="flex items-start space-x-4 p-4 bg-gray-50 rounded-lg">
                    <div class="flex-shrink-0">
                        <div class="w-10 h-10 bg-purple-100 rounded-full flex items-center justify-center">
                            <i class="fas fa-tag text-purple-600"></i>
                        </div>
                    </div>
                    <div>
                        <h4 class="font-bold text-gray-800 mb-1">SYN (Synchronize)</h4>
                        <p class="text-gray-600">同步标志位，用于发起连接请求和同步序列号。</p>
                    </div>
                </div>
                <div class="flex items-start space-x-4 p-4 bg-gray-50 rounded-lg">
                    <div class="flex-shrink-0">
                        <div class="w-10 h-10 bg-indigo-100 rounded-full flex items-center justify-center">
                            <i class="fas fa-check text-indigo-600"></i>
                        </div>
                    </div>
                    <div>
                        <h4 class="font-bold text-gray-800 mb-1">ACK (Acknowledgment)</h4>
                        <p class="text-gray-600">确认标志位，用于确认收到对方的数据包。</p>
                    </div>
                </div>
                <div class="flex items-start space-x-4 p-4 bg-gray-50 rounded-lg">
                    <div class="flex-shrink-0">
                        <div class="w-10 h-10 bg-purple-100 rounded-full flex items-center justify-center">
                            <i class="fas fa-hashtag text-purple-600"></i>
                        </div>
                    </div>
                    <div>
                        <h4 class="font-bold text-gray-800 mb-1">ISN (Initial Sequence Number)</h4>
                        <p class="text-gray-600">初始序列号，用于标识数据流的